import 'package:flutter/material.dart';
import 'package:flutter_study_app/widget/dropdown_menu/gzx_dropdown_header.dart';
import 'package:flutter_study_app/widget/dropdown_menu/gzx_dropdown_menu.dart';
import 'package:flutter_study_app/widget/dropdown_menu/gzx_dropdown_menu_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class PostOwnLendPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _PostOwnLendPageState();
}

class _PostOwnLendPageState extends State<PostOwnLendPage> {
  final List<String> _companyList = ['全部企业', '事业单位', '国有企业'];
  GlobalKey _stackKey = GlobalKey();
  GZXDropdownMenuController _dropdownMenuController =
      GZXDropdownMenuController();
  String _companyType = "全部企业";

  _buildSelectCompany() {
    double height = _companyList.length * 48.w;
    return GZXDropdownMenuBuilder(
        dropDownHeight: height,
        dropDownWidget: ListView.builder(
            itemCount: _companyList.length,
            itemBuilder: (context, index) {
              String item = _companyList[index];
              bool isSelected = item == _companyType;
              return InkWell(
                onTap: () {
                  _companyType = item;
                  _dropdownMenuController.hide();
                  setState(() {});
                },
                child: Container(
                  height: 48.w,
                  color: Colors.white,
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.only(left: 20.w, right: 20.w),
                  child: Row(
                    children: [
                      Text(
                        item,
                        style: TextStyle(
                            color: isSelected
                                ? Color(0xFF009669)
                                : Color(0xFF333333),
                            fontSize: 14.sp),
                      ),
                      Spacer(),
                      Visibility(
                          visible: isSelected,
                          child: Image.asset(
                            'assets/images/ic_selected_mark.png',
                            width: 16.w,
                            height: 16.w,
                          ))
                    ],
                  ),
                ),
              );
            }));
  }

  _buildItemView(int index) {
    return Container(
      margin: EdgeInsets.only(
          left: 12.w, right: 12.w, bottom: 12.w, top: index == 0 ? 12.w : 0),
      padding: EdgeInsets.all(12.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(4.w),
        color: Colors.white,
      ),
      child: Column(
        children: [
          Row(
            children: [
              Image.asset(
                'assets/images/ic_company_type01.png',
                width: 24.w,
                height: 24.w,
              ),
              SizedBox(
                width: 8.w,
              ),
              Expanded(
                child: Text(
                  '三一重工',
                  maxLines: 1,
                  textAlign: TextAlign.left,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                      fontSize: 16.sp,
                      color: Color(0xFF333333),
                      fontWeight: FontWeight.w500),
                ),
              ),
              Text(
                '¥22,000.00',
                style: TextStyle(
                    color: Color(0xFF333333),
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w500),
              )
            ],
          ),
          Container(
            height: 1.w,
            color: Color(0xFFEEEEEE),
            margin: EdgeInsets.only(top: 8.w, bottom: 10.w),
          ),
          Stack(
            children: [
              Column(
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Image.asset(
                        'assets/images/ic_client.png',
                        width: 16.w,
                        height: 16.w,
                      ),
                      Text(
                        '王梅梅',
                        style: TextStyle(
                            color: Color(0xFF666666), fontSize: 12.sp),
                      )
                    ],
                  ),
                  SizedBox(
                    height: 5.w,
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Image.asset(
                        'assets/images/ic_time.png',
                        width: 16.w,
                        height: 16.w,
                      ),
                      Text(
                        '2021-01-12 12:12',
                        style: TextStyle(
                            color: Color(0xFF666666), fontSize: 12.sp),
                      )
                    ],
                  ),
                ],
              ),
              Positioned(
                bottom: 0,
                right: 0,
                child: InkWell(
                    onTap: () {},
                    child: Container(
                      width: 100.w,
                      height: 24.w,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(12.w),
                          border:
                              Border.all(color: Color(0xFF009669), width: 1.w)),
                      child: Text(
                        '联系客户',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            fontSize: 13.sp, color: Color(0xFF009669)),
                      ),
                    )),
              ),
              Positioned(
                right: 0,
                bottom: 18.w,
                child: Container(
                  width: 30.w,
                  height: 15.w,
                  decoration: BoxDecoration(
                      color: Color(0xFFE1F4EE),
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(8.w),
                        topRight: Radius.circular(18.w),
                        bottomLeft: Radius.zero,
                        bottomRight: Radius.circular(8.w),
                      )),
                  alignment: Alignment.center,
                  child: Text(
                    '2次',
                    textAlign: TextAlign.center,
                    style: TextStyle(color: Color(0xFF009669), fontSize: 10.sp),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      key: _stackKey,
      children: [
        Column(
          children: [
            Container(
              height: 1.w,
              color: Color(0xFFEEEEEE),
            ),
            Container(
              height: 42.w,
              color: Colors.white,
              alignment: Alignment.centerLeft,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                    child: GZXDropDownHeader(
                      items: [
                        GZXDropDownHeaderItem(
                          _companyType,
                          itemAlign: MainAxisAlignment.start,
                          itemPadding: EdgeInsets.only(left: 12.w),
                          isCustomIcon: true,
                          iconName: 'assets/images/ic_dropmenu_arrow_down.png',
                          iconDropDownName:
                              'assets/images/ic_dropmenu_arrow_up.png',
                          iconSize: 12.w,
                          labelIconGap: 4.w,
                        ),
                      ],
                      borderWidth: 0,
                      dividerHeight: 0,
                      dividerColor: Colors.transparent,
                      borderColor: Colors.transparent,
                      controller: _dropdownMenuController,
                      stackKey: _stackKey,
                      height: 42.w,
                      style:
                          TextStyle(color: Color(0xFF333333), fontSize: 14.sp),
                      dropDownStyle: TextStyle(
                        fontSize: 14.sp,
                        color: Color(0xFF009669),
                      ),
                      boxWidth: MediaQuery.of(context).size.width / 3,
                    ),
                  ),
                  Expanded(
                      child: InkWell(
                    onTap: () {
                      if (_dropdownMenuController.isShow) {
                        _dropdownMenuController.hide();
                      }
                    },
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          '授信金额',
                          style: TextStyle(
                              color: Color(0xFF333333), fontSize: 14.sp),
                        ),
                        Image.asset(
                          'assets/images/ic_sort_normal.png',
                          width: 16.w,
                          height: 16.w,
                        ),
                      ],
                    ),
                  )),
                  Expanded(
                      child: InkWell(
                    onTap: () {
                      if (_dropdownMenuController.isShow) {
                        _dropdownMenuController.hide();
                      }
                    },
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Text(
                          '最近更新',
                          style: TextStyle(
                              color: Color(0xFF333333), fontSize: 14.sp),
                        ),
                        Image.asset(
                          'assets/images/ic_sort_normal.png',
                          width: 16.w,
                          height: 16.w,
                        ),
                        SizedBox(
                          width: 12.w,
                        ),
                      ],
                    ),
                  )),
                ],
              ),
            ),
            Expanded(
                child: ListView.builder(
              physics: BouncingScrollPhysics(),
              itemCount: 15,
              itemBuilder: (context, index) {
                return _buildItemView(index);
              },
            ))
          ],
        ),
        GZXDropDownMenu(
          // controller用于控制menu的显示或隐藏
          controller: _dropdownMenuController,
          // 下拉菜单显示或隐藏动画时长
          animationMilliseconds: 300,
          // 下拉后遮罩颜色
//          maskColor: Theme.of(context).primaryColor.withOpacity(0.5),
//          maskColor: Colors.red.withOpacity(0.5),
          dropdownMenuChanging: (isShow, index) {},
          dropdownMenuChanged: (isShow, index) {},
          // 下拉菜单，高度自定义，你想显示什么就显示什么，完全由你决定，你只需要在选择后调用_dropdownMenuController.hide();即可
          menus: [_buildSelectCompany()],
        ),
      ],
    );
  }
}
